package cn.com.wdjedu.androidapp.wjeduandroid.support.lib.imagesave.support;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import cn.com.wdjedu.androidapp.wjeduandroid.support.lib.pulltorefresh.Utility;

/**
 * 图片保存接口层实现 2017/1/14.
 */

public class ImageSaverBaseImpl implements ImageSaverBase {

    @Override
    public String saveImage(Bitmap bitmap, String path) {
        if (bitmap == null || bitmap.getByteCount() == 0) {
            return "";
        }
        boolean success = false;
        File file = new File(path);
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(file);
            success = bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            Utility.closeSilently(fos);
        }

        if (success) {
            return path;
        }
        return "";
    }

    @Override
    public String saveImage(Bitmap bitmap, String directory, String fileName) {
        String path = directory + File.separator + fileName; /*文件保存路径*/
        return saveImage(bitmap, path);
    }

    @Override
    public String saveImage(byte[] bytes, String path) {
        return saveImage(BitmapFactory.decodeByteArray(bytes, 0, bytes.length), path);
    }

    @Override
    public String saveImage(byte[] bytes, String directory, String fileName) {
        String path = directory + File.separator + fileName; /*文件保存路径*/
        return saveImage(bytes, path);
    }
}
